package com.goldplusgold.tradedelay.app.market.channel;

//import com.haotunet.app.goldtreasure.protobuf.HeartbeatInfo;
//import com.haotunet.app.goldtreasure.protobuf.Market;


import com.goldplusgold.network.lib.http.protobuf.HeartbeatInfo;
import com.goldplusgold.network.lib.http.protobuf.Market;

import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.handler.timeout.IdleState;
import io.netty.handler.timeout.IdleStateEvent;

/**
 * 行情心跳处理器
 */
public class MarketHeartbeatHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exception {

        if (evt instanceof IdleStateEvent) {
            IdleStateEvent event = (IdleStateEvent) evt;
            //读事件被触发
            if (event.state() == IdleState.READER_IDLE) {
                Market.Heartbeat heartbeat = Market.Heartbeat.newBuilder().
                        setInfo(HeartbeatInfo.REQUEST.toInfo()).build();
                Market.Identification identification = Market.Identification.newBuilder().
                        setCategory(Market.Category.HEARTBEAT).build();
                Market.Response data = Market.Response.newBuilder().setHeartbeat(heartbeat).
                        setIdentification(identification).build();
                ctx.channel().writeAndFlush(data).addListener(ChannelFutureListener.CLOSE_ON_FAILURE);
            }
        } else {
            super.userEventTriggered(ctx, evt);
        }
    }


}
